define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init();
            
            //绑定事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var panel = $($(this).attr("href"));
                if (panel.size() > 0) {
                    Controller.table[panel.attr("id")].call(this);
                    $(this).on('click', function (e) {
                        $($(this).attr("href")).find(".btn-refresh").trigger("click");
                    });
                }
                //移除绑定的事件
                $(this).unbind('shown.bs.tab');
            });
            
            //必须默认触发shown.bs.tab事件
            $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");
        },
        table: {
            first: function () {
                Table.api.init({
                    extend: {
                        index_url: 'travel/index',
                        add_url: '',
                        edit_url: 'travel/edit',
                        del_url: 'travel/del',
                        multi_url: '',
                        table: 'travel',
                    }
                },
                {
                    //0 已提交 等待审核；1 审核通过（生成活动中）；2 报名中；3 已结束；4 审核未通过;5 已取消
                    custom:{'报名中':'success','审核通过（生成活动中）':'success','待审核':'warning','审核未通过':'danger'}
                }
                );

                // 表格1
                var table1 = $("#index");
                table1.bootstrapTable({
                   // url: 'travel/index',
                    url: $.fn.bootstrapTable.defaults.extend.index_url,
                   /* extend: {
                        index_url: '',
                        add_url: '',
                        edit_url: 'travel/edit',
                        del_url: 'travel/del',
                        multi_url: '',
                        table: '',
                    },*/
                    //0 已提交 等待审核；1 审核通过（生成活动中）；2 报名中；3 已结束；4 审核未通过;5 已取消
                    toolbar: '#toolbar1',
                    sortName: 'id',
                    search: false,
                    columns: [
                        [
                            {field: 'id', title: __('Id'), operate:false},
                            {field: 'categorys',title:'类别', operate:false},
                            {field: 'titles',title:'名称', operate:false},
                            {field: 'user_scale', title: __('User_scale'),visible: false, operate:false},
                            {field: 'begintime', title: __('Begintime'), formatter: Table.api.formatter.datetime, operate: 'BETWEEN', type: 'datetime', addclass: 'datetimepicker', data: 'data-date-format="YYYY-MM-DD"'},
                            {field: 'endtime', title: __('Endtime'), formatter: Table.api.formatter.datetime, operate: 'BETWEEN', type: 'datetime', addclass: 'datetimepicker', data: 'data-date-format="YYYY-MM-DD"'},
                            {field: 'state', title: __('State'),formatter:Table.api.formatter.flag, searchList: {'0': '待审核', '1': '审核通过（生成活动中）','2':'报名中','3':'已结束','4':'审核未通过','5':'已取消'},},
                            {field: 'total_fee', title: __('Total_fee'),visible: false, operate:false,},
                            {field: 'cost', title: __('Cost'),visible: false, operate:false,},
                            {field: 'deposit', title: __('Deposit'),visible: false, operate:false,},
                            {field: 'group_num', title: __('Group_num'),visible: false, operate:false},
                            {field: 'limit_num', title: __('Limit_num'),visible: false, operate:false},
                            {field: 'memebr_price', title: __('Memebr_price'),visible: false, operate:false,},
                            {field: 'price', title: __('Price'),visible: false, operate:false,},
                            {field: 'commission', title: __('Commission'),visible: false, operate:false,},
                            {field: 'is_promote', title: __('Is_promote'),visible: false, operate:false},
                            {field: 'operate', title: __('Operate'), table: table1, 
                                buttons:[{
                                    name: 'aclist', 
                                    text: '报名名单', 
                                    classname: 'btn btn-xs btn-info btn-fields btn-addtabs', 
                                    icon: 'fa fa-list', 
                                    url: 'travelmember/index?travel_id={ids}'
                                },
                               /* {
                                    name: 'detail', 
                                    text: '详情', 
                                    classname: 'btn btn-xs btn-info btn-detail btn-dialog', 
                                    icon: 'fa fa-list', 
                                    url: 'supplier/detail'
                                }*/],
                                events: Table.api.events.operate, formatter: Table.api.formatter.operate
                            }
                        ]
                    ]
                });

                // 为表格1绑定事件
                Table.api.bindevent(table1);
            },
            second: function () {
                // 表格2
                var table2 = $("#oversea");
                table2.bootstrapTable({
                    url: 'travel/oversea',
                    extend: {
                        index_url: '',
                        add_url: '',
                        edit_url: 'travel/edit',
                        del_url: 'travel/del',
                        multi_url: '',
                        table: '',
                    },
                    toolbar: '#toolbar2',
                    sortName: 'id',
                    search: false,
                    columns: [
                        [
                            {field: 'id', title: __('Id'), operate:false},
                            {field: 'titles',title:'名称', operate:false},
                           {field: 'begintime', title: __('Begintime'), formatter: Table.api.formatter.datetime, operate: 'BETWEEN', type: 'datetime', addclass: 'datetimepicker', data: 'data-date-format="YYYY-MM-DD"'},
                            {field: 'endtime', title: __('Endtime'), formatter: Table.api.formatter.datetime, operate: 'BETWEEN', type: 'datetime', addclass: 'datetimepicker', data: 'data-date-format="YYYY-MM-DD"'},
                            {field: 'state', title: __('State'),formatter:Table.api.formatter.flag, searchList: {'0': '待审核', '1': '审核通过（生成活动中）','2':'报名中','3':'已结束','4':'审核未通过','5':'已取消'},},
                            {field: 'total_fee', title: __('Total_fee'),visible: false, operate:false},
                            {field: 'cost', title: __('Cost'),visible: false, operate:false},
                            {field: 'deposit', title: __('Deposit'),visible: false, operate:false},
                            {field: 'group_num', title: __('Group_num'),visible: false, operate:false},
                            {field: 'limit_num', title: __('Limit_num'),visible: false, operate:false},
                            {field: 'memebr_price', title: __('Memebr_price'),visible: false, operate:false},
                            {field: 'price', title: __('Price'),visible: false, operate:false},
                            {field: 'commission', title: __('Commission'),visible: false, operate:false},
                            {field: 'is_promote', title: __('Is_promote'),visible: false, operate:false},
                            {field: 'operate', title: __('Operate'), table: table2, 
                                buttons:[{
                                    name: 'aclist', 
                                    text: '报名名单', 
                                    classname: 'btn btn-xs btn-info btn-fields btn-addtabs', 
                                    icon: 'fa fa-list', 
                                    url: 'travelmember/index?travel_id={ids}'
                                },
                               /* {
                                    name: 'detail', 
                                    text: '详情', 
                                    classname: 'btn btn-xs btn-info btn-detail btn-dialog', 
                                    icon: 'fa fa-list', 
                                    url: 'supplier/detail'
                                }*/],
                                events: Table.api.events.operate, formatter: Table.api.formatter.operate
                            }
                        ]
                    ]
                });

                // 为表格2绑定事件
                Table.api.bindevent(table2);
            },
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
        }
    };
    return Controller;
});